package cn.dianchiyun.protocol.netty.dtu.entity;

import cn.dianchiyun.protocol.netty.utils.CRC16;
import cn.dianchiyun.protocol.netty.utils.HexUtil;
import org.apache.commons.lang.ArrayUtils;

import java.util.Arrays;

/**
 * 读数据
 */
public class DTURead {

	//设备注册码
	private String serialCode;

	//地址码
	private String address;

	//功能码
	private String funCode;

	//数据长度
	private int length;

	//数据内容
	private byte[] data;

	//数据内容
	private Integer[] datas;

	//	CRC16校验和
	private Integer crc16;

	//是否是心跳包
	private boolean heartPack = false;

	public DTURead() {

	}

	public DTURead(String serialCode, boolean heartPack) {
		super();
		this.serialCode = serialCode;
		this.heartPack = heartPack;
	}

	public DTURead(String serialCode, boolean heartPack, String address, String funCode, int length, byte[] data, Integer[] datas, Integer crc16) {
		super();
		this.serialCode = serialCode;
		this.heartPack = heartPack;
		this.address = address;
		this.funCode = funCode;
		this.length = length;
		this.data = data;
		this.datas = datas;
		this.crc16 = crc16;
	}

	public String getSerialCode() {
		return serialCode;
	}

	public void setSerialCode(String serialCode) {
		this.serialCode = serialCode;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFunCode() {
		return funCode;
	}

	public void setFunCode(String funCode) {
		this.funCode = funCode;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public Integer[] getDatas() {
		return datas;
	}

	public void setDatas(Integer[] datas) {
		this.datas = datas;
	}

	public Integer getCrc16() {
		return crc16;
	}

	public void setCrc16(Integer crc16) {
		this.crc16 = crc16;
	}

	public boolean isHeartPack() {
		return heartPack;
	}

	public void setHeartPack(boolean heartPack) {
		this.heartPack = heartPack;
	}

	public byte[] getBytes(){
		byte[] res = {};
		res = ArrayUtils.addAll(res, this.getAddress().getBytes());
		res = ArrayUtils.addAll(res, this.getFunCode().getBytes());
		res = ArrayUtils.addAll(res, HexUtil.intToHex16(this.getLength()).getBytes());
		res = ArrayUtils.addAll(res, this.getData());
		return res;
	}

	public String getString(){
		StringBuilder res = new StringBuilder();
		res.append(this.getAddress());
		res.append(this.getFunCode());
		res.append(HexUtil.intToHex16(this.getLength()));
		res.append(HexUtil.receiveHexToString(this.getData()));
		return res.toString();
	}

	public String getCRCString(){
		System.out.println("getCRCBytes():" + getString());
		byte[] sbuf = CRC16.getSendBuf(getString());
		return CRC16.getBufHexStr(sbuf);
	}


	@Override
	public String toString() {
		return "DTURead{" +
				"serialCode='" + serialCode + '\'' +
				", address='" + address + '\'' +
				", funCode='" + funCode + '\'' +
				", length=" + length +
				", data=" + Arrays.toString(data) +
				", datas=" + Arrays.toString(datas) +
				", crc16=" + crc16 +
				", heartPack=" + heartPack +
				'}';
	}

}
